<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            v-model="queryinfo.query"
            :clearable="true"
            placeholder="请输入内容"
          >
            <el-button
              @click="search"
              slot="append"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button @click="switch2" type="primary">添加用户 </el-button>
        </el-col>
      </el-row>

      <!-- 下面是用户列表渲染 分页过的数据  -->
      <el-table :data="userlist" style="width: 100%">
        <el-table-column label="序号" type="index" width="80"></el-table-column>
        <el-table-column
          label="用户名"
          prop="UserName"
          width="100"
        ></el-table-column>
        <el-table-column
          label="手机"
          prop="Mobile"
          width="180"
        ></el-table-column>
        <el-table-column
          label="邮箱"
          prop="Email"
          width="180"
        ></el-table-column>
        <el-table-column label="状态" prop="MgState" width="100">
          <template slot-scope="dfdf">
            <el-switch
              v-model="dfdf.row.MgState"
              active-color="#13ce66"
              @change="userstatechange(dfdf.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              @click="updateuserbtn(scope.row)"
              size="mini"
              type="success"
              >修改</el-button
            >
            <el-button size="mini" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页效果 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryinfo.pagenum"
      :page-size="queryinfo.pagesize"
      :page-sizes="[3, 5, 7, 9]"
      layout="total,sizes,prev, pager, next,jumper"
      :total="total"
    >
    </el-pagination>

    <!-- dialog1对话框表单 -->
    <el-dialog
      title="添加用户信息"
      :visible.sync="adduserdialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <el-form
        ref="addFormRef"
        :rules="addFormRules"
        :model="addForm"
        label-width="80px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="addForm.mobile"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="adduserdialogVisible = false">取消</el-button>
        <el-button type="primary" @click="enterl">确定</el-button>
      </span>
    </el-dialog>

    <!-- 修改用户用的dialog框  -->
    <!--修改用户信息对话框-->
    <el-dialog
      title="修改用户信息"
      :visible.sync="updateUserDialogVisible"
      width="40%"
      @close="updateDialogClosed"
    >
      <el-form
        ref="updateFormRef"
        :rules="addFormRules"
        :model="updateForm"
        label-width="80px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="updateForm.username"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="updateForm.password" disabled></el-input>
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input v-model="updateForm.email"></el-input>
        </el-form-item>

        <el-form-item label="电话" prop="mobile">
          <el-input v-model="updateForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateUserDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateUserenter">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: [],
  components: {},
  //    const {query,pagenum,pagesize} = req.query
  data() {
    return {
      userlist: [],
      // 这是添加用户用的 数据结构
      addForm: {
        username: "",
        password: "",
        email: "",
        mobile: "",
        // pagenum: 1,
        // pagesize: 3
      },
      // 修改用户用的数据结构
      updateForm: {
     
        username: "",
        password: "",
        email: "",
        mobile: "",
           // pagenum: 1,
        // pagesize: 3
      },
      total: 0,
      adduserdialogVisible: false,
      updateUserDialogVisible: false,
      queryinfo: { query: "", pagenum: 1, pagesize: 3 },
      addFormRules: {
        username: [
          //required 必填
          //message 信息提示
          //trigger 鼠标失去焦点时触发
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },

          {
            pattern: /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/,
            message: "输入正确邮箱规则22",
            trigger: "blur",
          },
        ],
        mobile: [
          { required: true, message: "请输入手机号码", trigger: "blur" },

          {
            pattern: /^1[3456789]\d{9}$/,
            message: "请输入正确手机号",
            trigger: "blur",
          },
        ],
      },
    };
  },
  //方法 函数写这里
  methods: {
    // 最后确定修改
    updateUserenter() {
      console.log('确定修改按钮')  
      this.$refs.updateFormRef.validate(async (valid)=>{
          console.log(' 修改的valid校验 valid',valid)
               this.updateForm.pagenum = this.queryinfo.pagenum;
          this.updateForm.pagesize = this.queryinfo.pagesize;
          const res = await this.$http.get('/api/updateuser',{
            params:this.updateForm
          })

          console.log('res修改成功返回主页数据',res)
          this.userlist = res.data.data 
          this.total = res.data.total 
          this.updateUserDialogVisible = false 
      })
    },
    // 把修改的dialog呼唤出来
    updateuserbtn(info) {
      console.log("updateuserbtn", info);
      this.updateForm.id = info.id;
      this.updateForm.username = info.UserName;
      this.updateForm.password = info.Password;
      this.updateForm.email = info.Email;
      this.updateForm.mobile = info.Mobile;
      this.updateUserDialogVisible = true;
    },
    // 监听dialog的消失事件的回调函数
    addDialogClosed() {
      console.log("  addDialogClosed 监听dialog的消失事件的回调函数");
      this.$refs.addFormRef.resetFields();
    },
    // 监听修改dialog的消失事件的回调函数
    updateDialogClosed() {
      console.log("   监听修改用户 dialog的消失事件的回调函数");
      this.$refs.updateFormRef.resetFields();
    },
    // 添加用户的确认按钮！
    enterl() {
      console.log("enter1");
      this.$refs.addFormRef.validate(async (valid) => {
        console.log("valid添加用户vlalid", valid);
        if (valid) {
          this.addForm.pagenum = this.queryinfo.pagenum;
          this.addForm.pagesize = this.queryinfo.pagesize;
          const res = await this.$http.get("/api/adduser", {
            params: this.addForm,
          });
          this.userlist = res.data.data;
          this.total = res.data.total;
          this.adduserdialogVisible = false;
        } else {
          console.log("err submit");
          return false;
        }
      });
    },
    // 切换 添加用户模态框隐藏出现
    switch2() {
      this.adduserdialogVisible = true;
    },
    // 搜索用户数据方法
    async search() {
      const res = await this.$http.get("/api/users2", {
        params: this.queryinfo,
      });
      this.userlist = res.data.data;
    },
    showinfo() {
      console.log(this.queryinfo);
    },
    handleSizeChange(newsize) {
      console.log("handleSizeChange 是每一页显示多少条后会自动触发", newsize);
      this.queryinfo.pagesize = newsize;
      this.getuserlist();
    },
    handleCurrentChange(newpage) {
      console.log(" handleCurrentChange 具体哪一页", newpage);
      this.queryinfo.pagenum = newpage;
      this.getuserlist();
    },
    async getuserlist() {
      const res = await this.$http.get("/api/users", {
        params: this.queryinfo,
      });
      console.log("userlist silce", res);
      this.userlist = res.data.data;
      this.total = res.data.total;
      console.log("thisuserlist", this.userlist);
    },
    async userstatechange(userinfo) {
      console.log("userinfo", userinfo);
      const res = await this.$http("/api/userupdate", { params: userinfo });
      //  res.json ({
      //                 "code":"200",
      //                 "newState":user.MgState
      //             })
      console.log("res", res);
      if (res.data.code != 200) {
        this.$message("请求没成功");
        return;
      }
      console.log("newstate", res.data.newState);
      if (res.data.newState == "true") {
        this.$message.success("现在是正常状态");
        return;
      } else {
        this.$message("现在是封禁状态");
      }
    },
  },
  //计算属性
  computed: {},
  //侦听器
  watch: {},
  //过滤器
  filters: {},
  //以下是生命周期
  //组件创建之前
  beforeCreate() {},
  //组件创建之后
  created() {
    this.getuserlist();
  },
  //页面渲染之前
  beforeMount() {},
  //页面渲染之后
  mounted() {},
  //页面视图数据更新之前
  beforeUpdate() {},
  //页面视图数据更新之后
  updated() {},
  //页面销毁之前
  beforeDestroy() {},
  //页面销毁之后
  destroyed() {},
};
</script>


<style lang="scss"  scoped>
.el-card {
  margin-top: 20px;
  box-shadow: 0 2px 2px #121a2a;
}

.el-breadcrumb {
  font-size: 12px;
}

.eltable {
  width: 100% !important;
}
</style>